package controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import client.CcrmApp;
import model.CustomerListModel;
import model.Employee;
import model.Envelope;
import model.User;
import view.AcceptOrRejectGui;
import view.GetCustomerListGui;
/**
 * this class control on choose customer from list 
 * @author LIRAN
 *
 */
public class GetCustomerListConntroler extends AbstractTransfer {
	/**
	 * for back to previose frame
	 */
	private SalesManControllers backcon = null;
	/**
	 * this controller
	 */
	private GetCustomerListConntroler tempcon = null;
	/**
	 * the gui of this controller
	 */
	private GetCustomerListGui ListGui = null;
	/**
	 * the employee who is conected to the system 
	 */
	private  Employee emp = null;
	/**
	 * for log out
	 */
	private LoginController logout1 = null;
	/**
	 * to save some parameters 
	 */
	private CustomerListModel CListM = null;
	/**
	 * send user to next frame
	 */
	private User user1 = null;

	
	/**
	 * constructor 
	 * @param s->SalesManControllers
	 * @param g->GetCustomerListGui
	 * @param e->Employee
	 * @param out->LoginController
	 */
	public GetCustomerListConntroler(SalesManControllers s,GetCustomerListGui g,Employee e,LoginController out){
		super(g,e);
		backcon = s;
		tempcon =this;
		emp = e;
		ListGui = g;
		logout1 = out;

		initcomboBox();
		ListGui.addComboCampAc(new CampainComboBox());
		ListGui.addbtnBackAL(new Backtosale());
		ListGui.addbtnlogoutAL(new Logoutsale());

		ListGui.addbtnNextAL(new Nextbottob());
	}
	/**
	 * init combo box
	 */
	public void initcomboBox()
	{
		String cmp = new String();
		cmp= "";
		Envelope ev =  new Envelope((String)cmp,"Take Campain");
		CcrmApp.clien.setCurrObj(tempcon);
		sendToServer(ev);
	}

	/**
	 * for log out status=0
	 */
	public void UpdateDB0(){
		 Envelope en1 = new Envelope(emp,"updateStatus0");
		 sendToServer(en1);
	} 
	


	/**
	 * back to main menu 
	 * @author liran
	 *
	 */
	class Backtosale implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			ListGui.dispose();
			backcon.GetSalesManGui().setVisible(true);
		}
	}
	
	/**
	 * for logout
	 * @author liran
	 *
	 */
	class Logoutsale implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			UpdateDB0();
			ListGui.dispose();
			logout1.getLoginG().ClearText();
			logout1.getLoginG().setVisible(true);

		}
	}
	
	/**
	 * if u choose some thing on combo box ...
	 * @author liran
	 *
	 */
	class CampainComboBox implements ItemListener{

		@Override
		public void itemStateChanged(ItemEvent e) {
			// TODO Auto-generated method stub
			String s= new String();
			s = ListGui.getSeclectItemS();
	    
		    if (s.equals(" ") == false){
			String[] q=s.trim().split("\\.");
			
		    CustomerListModel CListMod = new CustomerListModel(q[1]);
			DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
			Calendar dd = Calendar.getInstance();

			CListMod.setCurrDate(dateFormat.format(dd.getTime()));
			Envelope ev = new Envelope(CListMod, "Take a Customer List");
			CcrmApp.clien.setCurrObj(tempcon);
			sendToServer(ev);
			  ListGui.getCustomercomboBox().setEnabled(true);
		}
		}
		
	}

	/**
	 * insert customer to combo box 
	 * @param m-> list of customers
	 */
	public void handlemessformDBCustomer(CustomerListModel m)
	{
	    ListGui.getCustomercomboBox().removeAllItems();
	    System.out.println(m.getCD().size());
	   		for (int i =0 ; i < m.getCD().size();i++)
		{
		ListGui.setItemCustComboBOx(m.getCD().get(i).getFirstname()+" "+m.getCD().get(i).getLasttname()+"-"+m.getCD().get(i).getUid());
		}
		CListM = m;
	}
	
	
	/**
	 * insert list of campign to combo box
	 * @param m->list campign
	 */
	public void handlemessformDBCampain(String m)
	{
		String[] q=m.trim().split(",");

		for (int i=0;i< q.length ;i++)
		{
			ListGui.setItemCampComboBOx("Campain No."+q[i]);
		}
	}
	/**
	 * for continue in sale
	 * @author liran
	 *
	 */
	class Nextbottob implements ActionListener{

		@Override

		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			String s = ListGui.getSeclectCustomerItemS();
			System.out.println("====="+s+"=====");
			if (s != null)
			{
				
			String[] q=s.trim().split("-");
			String[] q1=q[0].trim().split(" ");
			System.out.println(CListM.getCD().size());
			System.out.println("================");
			
			System.out.println(CListM.getCD().size());
			for (int i=0 ; i< CListM.getCD().size();i++)
			{
				if(CListM.getCD().get(i).getUid().equals(q[1])){
					user1 = new User(q[1],q1[0],q1[1]);
				}
			}
			ListGui.dispose();
			AcceptOrRejectGui AoR = new AcceptOrRejectGui(CListM.getDescibe());
			AcceptOrRejectController AoRCon = new AcceptOrRejectController(user1, AoR, backcon,CListM.getCampID(),CListM.getCurrDate());
			}
		}
		
	}
	

	
	
	

}
